package com.ruoyi.pachong.service.impl;

import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.lang3.time.StopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.ruoyi.pachong.mapper.HpageNewsMapper;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.aspectj.LogAspect;
import com.ruoyi.pachong.domain.HpageDaohang;
import com.ruoyi.pachong.domain.HpageNews;
import com.ruoyi.pachong.service.IHpageNewsService;

/**
 * 个人首页新闻Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-03-22
 */
@Service
public class HpageNewsServiceImpl implements IHpageNewsService 
{
	private static final Logger log = LoggerFactory.getLogger(HpageNewsServiceImpl.class);
	
    @Autowired
    private HpageNewsMapper hpageNewsMapper;
    
    @Autowired
    private RedisCache redisCache;
    protected static final String newsKey = "hp-allnewskey";
    /**
     * 查询个人首页新闻
     * 
     * @param newId 个人首页新闻主键
     * @return 个人首页新闻
     */
    @Override
    public HpageNews selectHpageNewsByNewId(Long newId)
    {
        return hpageNewsMapper.selectHpageNewsByNewId(newId);
    }

    
    public Long selectHpageLastNewId() {
    	return hpageNewsMapper.selectHpageLastNewId();
    }
    /**
     * 查询个人首页新闻列表
     * 
     * @param hpageNews 个人首页新闻
     * @return 个人首页新闻
     */
    @Override
    public List<HpageNews> selectHpageNewsList(HpageNews hpageNews)
    {
        return hpageNewsMapper.selectHpageNewsList(hpageNews);
    }
    
    //@Cacheable(cacheNames="allnews")
    @Override
    public Map selectHpageNewsList(String uuid)
    {
    	 StopWatch stopWatch = new StopWatch();
         stopWatch.start();
    	//List<HpageNews> list = redisCache.getCacheList(newsKey);
    	Map<String, Map<String,List<HpageNews>>> groupByHpageNews = redisCache.getCacheObject(newsKey);
    	
   	 if (StringUtils.isNotNull(groupByHpageNews) && StringUtils.isNotEmpty(groupByHpageNews)) {
   		 
   	 }else {
   		 System.out.println("zou  sql  le ");
//   		 list = hpageNewsMapper.selectHpageNewsAllList(uuid);
//   		 redisCache.setCacheList(newsKey, list);
//   		 redisCache.expire(newsKey, 100);
   		 
   		log.info("新闻走了sql查询");
   		 //---------
   		Long lastid = this.selectHpageLastNewId();
        System.out.println("lastid:"+lastid);
        HpageNews  hpnew = this.selectHpageNewsByNewId(lastid);
        
        HpageNews hp = new HpageNews();
        hp.setNewUuid(hpnew.getNewUuid());
       
//        hpageNewsService.selectHpageNewsList(hp);
        List<HpageNews>  newslist = hpageNewsMapper.selectHpageNewsAllList(hpnew.getNewUuid());
        stopWatch.stop();
        System.out.println("耗时计算1："+(double) stopWatch.getTime()/1000 + "s");
        log.info("sql查询耗时："+(double) stopWatch.getTime()/1000 + "s");
        stopWatch.reset();
        stopWatch.start();
        groupByHpageNews = newslist.stream().sorted(Comparator.comparing(HpageNews::getClassOrder).thenComparing(HpageNews::getSubSiteOrder)).collect(Collectors.groupingBy(HpageNews::getClassName,LinkedHashMap::new,Collectors.groupingBy(HpageNews::getSubSiteName,LinkedHashMap::new, Collectors.toList())));
        stopWatch.stop();
        System.out.println("排序查询结果耗时："+(double) stopWatch.getTime()/1000 + "s");
        log.info("排序查询结果耗时："+(double) stopWatch.getTime()/1000 + "s");
        stopWatch.reset();
        stopWatch.start();
		 //redisCache.setCacheMap(newsKey, groupByHpageNews);
		 //redisCache.setCacheObject(newsKey, groupByHpageNews);
		 //redisCache.expire(newsKey, 100); 
		 stopWatch.stop();
	     System.out.println(" redis 存储耗时："+(double) stopWatch.getTime()/1000 + "s");
	     log.info("redis 存储耗时："+(double) stopWatch.getTime()/1000 + "s");
   		 
   	 }
   	 
   	
        return groupByHpageNews;
    }
    
    /**
     * 新增个人首页新闻
     * 
     * @param hpageNews 个人首页新闻
     * @return 结果
     */
    @Override
    public int insertHpageNews(HpageNews hpageNews)
    {
        return hpageNewsMapper.insertHpageNews(hpageNews);
    }

    /**
     * 修改个人首页新闻
     * 
     * @param hpageNews 个人首页新闻
     * @return 结果
     */
    @Override
    public int updateHpageNews(HpageNews hpageNews)
    {
        return hpageNewsMapper.updateHpageNews(hpageNews);
    }

    /**
     * 批量删除个人首页新闻
     * 
     * @param newIds 需要删除的个人首页新闻主键
     * @return 结果
     */
    @Override
    public int deleteHpageNewsByNewIds(Long[] newIds)
    {
        return hpageNewsMapper.deleteHpageNewsByNewIds(newIds);
    }

    /**
     * 删除个人首页新闻信息
     * 
     * @param newId 个人首页新闻主键
     * @return 结果
     */
    @Override
    public int deleteHpageNewsByNewId(Long newId)
    {
        return hpageNewsMapper.deleteHpageNewsByNewId(newId);
    }
}
